<template>
	<div class="container">
		<el-card shadow="hover">
			<div style="padding: 0 0 10px 0;">
				<el-button
					:disabled="!!(auth.indexOf('system.menu.add') > -1)"
					size="small"
					type="danger"
					class="ml10"
					@click="onOpenAdd"
				>添加</el-button>
			</div>
			<el-table
				:data="tableData.data"
				:tree-props="{ children: 'Children' }"
				row-key="Id"
				style="width: 100%"
			>
				<el-table-column label="菜单名称" align="left" width="150" show-overflow-tooltip>
					<template #default="scope">
						<i :class="scope.row.Icon"></i>
						<span class="ml10">{{ scope.row.MenuName }}</span>
					</template>
				</el-table-column>
				<el-table-column prop="Type" align="center" label="菜单类型" show-overflow-tooltip>
					<template #default="scope">
						<el-tag effect="dark" type="warning" v-if="scope.row.Type === 0">目录</el-tag>
						<el-tag effect="dark" type="danger" v-if="scope.row.Type === 1">菜单</el-tag>
						<el-tag effect="dark" v-if="scope.row.Type === 2">按钮</el-tag>
					</template>
				</el-table-column>
				<el-table-column align="center" label="权限标识" width="180" show-overflow-tooltip>
					<template #default="scope">
						<span>{{ scope.row.Permission }}</span>
					</template>
				</el-table-column>
				<el-table-column align="center" prop="RoutePath" label="路由名称" show-overflow-tooltip width="150">
					<template #default="scope">
						<span>{{ scope.row.RoutePath.substr(1) }}</span>
					</template>
				</el-table-column>
				<el-table-column align="center" label="组件地址" width="180" show-overflow-tooltip>
					<template #default="scope">
						<span>{{ scope.row.AssemblyPath.substr(1) }}</span>
					</template>
				</el-table-column>
				<el-table-column align="center" label="图标" show-overflow-tooltip>
					<template #default="scope">
						<i :class="scope.row.Icon"></i>
					</template>
				</el-table-column>
				<el-table-column prop="Status" align="center" label="状态" show-overflow-tooltip>
					<template #default="scope">
						<el-tag effect="dark" type="famili" v-if="scope.row.Status === 0">正常</el-tag>
						<el-tag effect="dark" type="danger" v-if="scope.row.Status === 1">禁用</el-tag>
					</template>
				</el-table-column>
				<el-table-column label="操作" show-overflow-tooltip width="150">
					<template #default="scope">
						<el-button
							:disabled="!!(auth.indexOf('system.menu.edit') > -1)"
							size="mini"
							type="text"
							v-if="scope.row.Type === 1 && scope.row.Children === null"
							@click="gen_basic(scope.row)"
						>生成基础权限</el-button>
						<el-button
							:disabled="!!(auth.indexOf('system.menu.edit') > -1)"
							size="mini"
							type="text"
							@click="onOpenEdit(scope.row)"
						>编辑</el-button>
						<!-- <el-button
							size="mini"
							type="text"
							v-if="scope.row.Status === 1"
							@click="onEnable(scope.row.Id)"
						>启用</el-button>
						<el-button
							size="mini"
							type="text"
							v-if="scope.row.Status === 0"
							@click="onDisable(scope.row.Id)"
						>禁用</el-button> -->
					</template>
				</el-table-column>
			</el-table>
		</el-card>
		<Add @sendRefresh="initTableData" :treeList="treeList" ref="addRef" />
		<Edit @sendRefresh="initTableData" :treeList="treeList" ref="editRef" />
	</div>
</template>

<script lang="ts">
import { ref, toRefs, reactive, defineComponent } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import { useStore } from '/@/store/index';
import Add from './component/add.vue';
import Edit from './component/edit.vue';
import { setBackEndControlRefreshRoutes } from "/@/router/backEnd";
import { getMenuTree, add_all, changeStatus } from '/@/api/platform/menu/index'
export default defineComponent({
	name: 'Menu',
	components: { Add, Edit },
	setup() {
		const store = useStore();
		const auth = store.state.userInfos.userInfos.authPageList;
		const addRef = ref();
		const editRef = ref();
		const state: any = reactive({
			tableData: {
				data: [],
				total: 0,
				loading: false,
				param: {
					pageNum: 1,
					pageSize: 10,
				},
			},
		});
		const treeList = ref([])
		const initTableData = () => {
			getMenuTree({ parentID: 0 }).then((res) => {
				state.tableData.data = res.Extras;
				state.tableData.total = res.Extras.length;
				treeList.value = state.tableData.data
			})
		};
		initTableData();
		// 打开新增菜单弹窗
		const onOpenAdd = (row: object) => {
			addRef.value.openDialog(row);
		};
		// 打开编辑菜单弹窗
		const onOpenEdit = (row: object) => {
			editRef.value.openDialog(row);
		};
		const gen_basic = (row: object) => {
			add_all({ permission: row.Permission, parent_id: row.ID }).then((res) => {
				if (res.code === 200) {
					ElMessageBox.alert(res.msg, '提示', {}).then(() => {
						initTableData();
						setBackEndControlRefreshRoutes() // 刷新菜单
					}).catch(() => { });
				}
			})
		};
		const onEnable = (id: any) => {
			ElMessageBox({
				closeOnClickModal: false,
				closeOnPressEscape: false,
				title: '提示',
				message: '确定启用此用户?',
				showCancelButton: true,
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				beforeClose: (action, instance, done) => {
					if (action === 'confirm') {
						changeStatus({ id: id, status: 0 }).then((res) => {
							ElMessage({
								message: '启用成功！',
								type: 'success',
								duration: 500
							})
							done();
							initTableData();
						})
					} else {
						done();
					}
				},
			})

		};
		const onDisable = (id: any) => {
			ElMessageBox({
				closeOnClickModal: false,
				closeOnPressEscape: false,
				title: '提示',
				message: '确定禁用此用户?',
				showCancelButton: true,
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				beforeClose: (action, instance, done) => {
					if (action === 'confirm') {
						changeStatus({ id: id, status: 1 }).then((res) => {
							ElMessage({
								message: '禁用成功！',
								type: 'success',
								duration: 500
							})
							done();
							initTableData();
						})
					} else {
						done();
					}
				}
			})
		};
		return {
			onEnable, onDisable,
			addRef,
			editRef,
			onOpenAdd,
			onOpenEdit,
			initTableData,
			gen_basic,
			...toRefs(state),
			treeList,
			auth
		};
	},
});
</script>
